package com.yycx.module.bus.provider.strategy.impl;

import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.groovy.provider.script.MyGroovyScriptEngine;
import com.yycx.module.bus.client.entity.hy.BusPolicyPosition;
import com.yycx.module.bus.client.entity.hy.BusPolicySaleman;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import com.yycx.module.bus.client.enums.HySaleTypeEnum;
import com.yycx.module.bus.provider.service.hy.BusPolicyPositionService;
import com.yycx.module.bus.provider.service.hy.BusPolicySalemanService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.bus.provider.strategy.SettleStrategy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 渠道商结算规则
 *
 * @author zyf
 */
@Service("settleStrategyF")
public class SettleStrategyF implements SettleStrategy {

    @Resource
    private BusSalemanService saleManService;

    @Resource
    private BusPolicyPositionService policyPositionService;

    @Resource
    private BusPolicySalemanService policySalemanService;

    @Resource
    private MyGroovyScriptEngine groovyScriptEngine;

    @Override
    public void setSettlePolicy(HyOrderDetail orderDetail, HyOrder hyOrder, Long saleManId) {
        //自定义结算政策
        Integer customPolicy = FlymeUtils.getInteger(orderDetail.getCustomPolicy(), 0);
        //订单类型
        String saleType = orderDetail.getSaleType();
        //查询结算对象
        BusSaleman saleManF = saleManService.getById(saleManId);
        //职务ID
        Long positionId = saleManF.getPositionId();
        //发货日期
        Date sendDate = orderDetail.getSendDate();
        //结算政策ID
        Long policyDrugId = orderDetail.getPolicyDrugId();
        BigDecimal settlePrice = orderDetail.getSettlePrice();
        BigDecimal settleRate = orderDetail.getSettleRate();
        //渠道商结算公式
        String settleFormulaContent = FlymeUtils.getString(orderDetail.getSettleFormulaContent(), "(销售单价-渠道商结算价)*数量*(1-渠道商标准);");
        //部门利润公式
        String deptProfitAmountFormula = FlymeUtils.getString(orderDetail.getDeptProfitAmountFormula(), "(渠道商结算价-部门结算价)*数量*(1-部门标准);");
        //税率差公式
        String taxRateDiffFormula = "(销售单价-渠道商结算价)*数量*((1-部门标准)-(1-渠道商标准));";
        Integer hasTaxRateDiff = 1;
        if (customPolicy.equals(0)) {
            //获取职务结算政策
            BusPolicyPosition busPolicyPosition = policyPositionService.findBusPolicyPosition(policyDrugId, positionId, sendDate);
            if (FlymeUtils.isNotEmpty(busPolicyPosition)) {
                settlePrice = FlymeUtils.getBigDecimal(busPolicyPosition.getPositionPrice(), settlePrice);
                settleRate = FlymeUtils.getBigDecimal(busPolicyPosition.getPositionRate(), settleRate);
                //价格取值方式
                Integer priceType = FlymeUtils.getInteger(busPolicyPosition.getPriceType(), 1);
                if (priceType.equals(2)) {
                    //使用成本价
                    settlePrice = FlymeUtils.getBigDecimal(orderDetail.getCostPrice(), settlePrice);
                }
            }

            //获取业务员结算政策
            BusPolicySaleman policySaleMan = policySalemanService.findByPolicyDrugIdAndSaleManId(policyDrugId, saleManId, sendDate);
            if (FlymeUtils.isNotEmpty(policySaleMan)) {
                settlePrice = FlymeUtils.getBigDecimal(policySaleMan.getSaleManPrice(), settlePrice);
                settleRate = FlymeUtils.getBigDecimal(policySaleMan.getSaleManRate(), settleRate);
                //非票折
                if (!HySaleTypeEnum.Pz.getCode().equals(saleType)) {
                    settleFormulaContent = FlymeUtils.getString(policySaleMan.getSaleManSettleFormula(), settleFormulaContent);
                }
                //价格取值方式
                Integer priceType = FlymeUtils.getInteger(policySaleMan.getPriceType(), 1);
                if (priceType.equals(2)) {
                    //使用成本价
                    settlePrice = FlymeUtils.getBigDecimal(orderDetail.getCostPrice(), settlePrice);
                }
                //结算规则(回款后结算)
                Integer settleRule = FlymeUtils.getInteger(policySaleMan.getSettleRule(), 1);
                //是否包票(默认包票)
                Integer baopiao = FlymeUtils.getInteger(policySaleMan.getBaopiao(), 1);
                orderDetail.setBaopiao(baopiao);
                orderDetail.setSettleRule(settleRule);
                //每盒提成金额
                BigDecimal rebateAmount = FlymeUtils.getBigDecimal(policySaleMan.getRebateAmount(), "0");
                orderDetail.setRebateAmount(rebateAmount);
                orderDetail.setRebateSettleFormula(policySaleMan.getRebateSettleFormula());
                Integer settleAmountType = policySaleMan.getSettleAmountType();
                orderDetail.setSettleAmountType(settleAmountType);
                //是否有税率差
                hasTaxRateDiff = FlymeUtils.getInteger(policySaleMan.getHasTaxRateDiff(), 1);
                hyOrder.setHasTaxRateDiff(hasTaxRateDiff);
            }
        }
        orderDetail.setSettlePrice(settlePrice);
        orderDetail.setSettleRate(settleRate);
        orderDetail.setSettleFormulaContent(settleFormulaContent);
        orderDetail.setSaleManIdF(saleManId);
        //部门利润公式
        orderDetail.setDeptProfitAmountFormula(deptProfitAmountFormula);
        if (FlymeUtils.gtzero(settleRate)) {
            if (hasTaxRateDiff.equals(1)) {
                //税率差公式
                orderDetail.setTaxRateDiffFormula(taxRateDiffFormula);
            } else {
                orderDetail.setTaxRateDiffFormula(null);
            }
        } else {
            orderDetail.setDeptProfitAmountFormula("部门费用-渠道商结算金额;");
            orderDetail.setTaxRateDiffFormula(null);
        }
    }

    @Override
    public void setSettleAmount(HyOrderDetail hyOrderDetail, HyOrder hyOrder, Map<String, Object> variable) {
        //渠道商结算价
        BigDecimal settlePrice = FlymeUtils.getBigDecimal(hyOrderDetail.getSettlePrice(), "0");
        //渠道商结算费率
        BigDecimal settleRate = FlymeUtils.getBigDecimal(hyOrderDetail.getSettleRate(), "0");
        //销售数量
        BigDecimal saleNum = FlymeUtils.getBigDecimal(hyOrderDetail.getSaleNum(), "0");
        //销售价
        BigDecimal salePrice = hyOrderDetail.getSalePrice();
        //高开金额
        BigDecimal multiply = (salePrice.subtract(settlePrice)).multiply(saleNum);
        if (FlymeUtils.gtzero(settlePrice)) {
            //结算发货金额(相当于资保金中的底价)
            BigDecimal settleSendAmount = settlePrice.multiply(saleNum).setScale(2, BigDecimal.ROUND_HALF_UP);
            hyOrderDetail.setSettleSendAmount(settleSendAmount);
            BigDecimal settleAmount = new BigDecimal("0");
            //默认计算(销售单价-市场结算价)*数量*(1-市场结算标准)
            settleAmount = multiply.multiply(new BigDecimal("1").subtract(settleRate));
            //渠道商结算公式
            String settleFormula = hyOrderDetail.getSettleFormulaContent();
            //使用公式重新计算渠道商结算金额
            if (FlymeUtils.isNotEmpty(settleFormula)) {
                settleAmount = groovyScriptEngine.executeBigDecimal(settleFormula, variable,2);
            }
            variable.put("渠道商结算金额", settleAmount);
            hyOrderDetail.setSettleAmount(settleAmount);
        }
        //渠道商提成单价
        BigDecimal rebateAmount = FlymeUtils.getBigDecimal(hyOrderDetail.getRebateAmount(), "0");
        if (FlymeUtils.gtzero(rebateAmount)) {
            BigDecimal settleReabteAmount = new BigDecimal("0");
            //渠道商提成公式
            String rebateSettleFormula = hyOrderDetail.getRebateSettleFormula();
            //使用公式重新计算渠道商结算金额
            if (FlymeUtils.isNotEmpty(rebateSettleFormula)) {
                settleReabteAmount = groovyScriptEngine.executeBigDecimal(rebateSettleFormula, variable,2);
            }
            variable.put("提成金额", settleReabteAmount);
            hyOrderDetail.setSettleRebateAmount(settleReabteAmount);
            hyOrder.setRebateState(1);
        } else {
            hyOrderDetail.setSettleRebateAmount(rebateAmount);
            hyOrder.setRebateState(0);
        }

    }
}
